using System;
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;

namespace FARAgent
{
    static class Program
    {
        private static Boolean RunAsAgent;
        private static List<string> m_ListOfCriterias;
        private static List<string> m_ListOfFiles;
        private static string m_Path;
        private static string m_InputExtension = @".mapping";
        private static string m_OutputExtension = @".rtxt";

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {

            if (args.Length > 0)
            {
                m_Path = args[0].ToString();
                if (Directory.Exists(m_Path))
                {
                    m_ListOfFiles = new List<string>();
                    foreach (string m_FileName in Directory.GetFiles(m_Path))
                        if (m_FileName.ToLower().EndsWith(m_InputExtension))
                            m_ListOfFiles.Add(m_FileName);

                    RunAsAgent = true;
                }
                else
                {
                    MessageBox.Show("To run as agent, a input path is needed.");
                }
            }

            if (RunAsAgent)
            {
                FAREngine m_FAREngine;

                foreach (string m_FileName in m_ListOfFiles)
                {
                    System.Console.WriteLine(m_FileName);

                    // Read Criteria File
                    m_ListOfCriterias = new List<string>();
                    using (StreamReader r = new StreamReader(m_FileName))
                    {
                        string line;
                        while ((line = r.ReadLine()) != null)
                        {
                            m_ListOfCriterias.Add(line);
                        }
                    }

                    // Create the destiny File
                    using (TextWriter tw = new StreamWriter(m_FileName.Replace(m_InputExtension, m_OutputExtension)))
                    {
                        // Open the source File
                        using (StreamReader r = new StreamReader(m_FileName.Replace(m_InputExtension, "")))
                        {
                            string line;
                            while ((line = r.ReadLine()) != null)
                            {
                                if (!line.Equals(""))
                                {
                                    string _Content = line;
                                    foreach (string criteria in m_ListOfCriterias)
                                    {
                                        m_FAREngine = new FAREngine(criteria);

                                        _Content = m_FAREngine.ReplaceInContent(_Content);

                                        //if (_Content != line)
                                        //{
                                        //    break;
                                        //}
                                    }

                                    tw.WriteLine(_Content);

                                } else
                                    tw.WriteLine(line);
                            }
                        }
                    } 
                }
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new FARForm());
            }
        }
    }
}